/**
 * @file myuart.c
 *
 * @ingroup    uart5 
 *
 * @brief      uart5 相关
 *
 * @author     Rq_wcl
 * @date       2022
 */


#include "myuart.h"

uint8_t uart5_tx_buff[128];



/**
 * @brief      uart5 init
 */
void uart5Init()
{
    FL_UART_InitTypeDef uart5InitStruct;
    FL_GPIO_InitTypeDef uart5GpioInitStruct;
    
    FL_GPIO_StructInit(&uart5GpioInitStruct);
    FL_UART_StructInit(&uart5InitStruct);
    
    uart5InitStruct.baudRate=115200;
    uart5InitStruct.dataWidth=FL_UART_DATA_WIDTH_8B;
    uart5InitStruct.stopBits=FL_UART_STOP_BIT_WIDTH_1B;
    uart5InitStruct.parity=FL_UART_PARITY_NONE;
    uart5InitStruct.transferDirection=FL_UART_DIRECTION_TX_RX;
    
    uart5GpioInitStruct.pin = FL_GPIO_PIN_0 | FL_GPIO_PIN_1;
    uart5GpioInitStruct.mode = FL_GPIO_MODE_DIGITAL;
    uart5GpioInitStruct.outputType=FL_GPIO_OUTPUT_PUSHPULL;
    uart5GpioInitStruct.remapPin=FL_DISABLE;
    // uart5GpioInitStruct.pull = FL_ENABLE;
    
    FL_GPIO_Init(GPIOD,&uart5GpioInitStruct);
    
    FL_UART_Init(UART5,&uart5InitStruct);
    FL_UART_ClearFlag_RXBuffFull(UART5);
    FL_UART_EnableIT_RXBuffFull(UART5);
    
    NVIC_EnableIRQ(UART5_IRQn);
}


/**
 * @brief      Enables the uart 5.
 */
void enableUart5()
{
    FL_RCC_EnableGroup3BusClock(FL_RCC_GROUP3_BUSCLK_UART5);
}


/**
 * @brief      Disables the uart 5.
 */
void disableUart5()
{
    FL_RCC_DisableGroup3BusClock(FL_RCC_GROUP3_BUSCLK_UART5);
}





/**
 * @brief      uart5 中断服务函数   
 * 
 * 这里主要是处理接收中断
 */
void UART5_IRQHandler()
{
    if(FL_UART_IsActiveFlag_RXBuffFull(UART5))
    {
        // flag read clear
        
    }
}



/**
 * @brief      uart5 发送字符串
 *
 * @param      data  发送字符串首指针
 */
void uart5Tx(uint8_t *data)
{
    uint8_t i;
    uint8_t len;
    len = strlen((const char*)data);
    for(i=0;i<len;i++)
    {
        myUartTxByte(*(data+i));
    }
}

/**
 * @brief      uart5 发送一个字节
 *
 * @param[in]  txData  The transmit data
 *
 * @return     结果
 */
uint8_t myUartTxByte(uint8_t txData)
{
    FL_UART_WriteTXBuff(UART5,txData);
    FL_DelayUsStart(1000);
    while(!FL_DelayEnd())
    {
        if (FL_UART_IsActiveFlag_TXShiftBuffEmpty(UART5))
        {
            return 1;
        }
        //cleanWDT();
    }
    return 0;
}





/**
 * @brief      uart5发送一个字符串升级版
 *
 * @param      fmt        式样化字符串
 * @param[in]  <unnamed>  参数表
* @attention  注意字符串长度不要超过 @see uart5_tx_buff 最大长度
 */
void uart5_printf(char *fmt,...)
{
    va_list ap;
    memset(uart5_tx_buff,0,sizeof(uart5_tx_buff));
    va_start(ap,fmt);
    vsprintf((char*)uart5_tx_buff,fmt,ap);
    va_end(ap);
    uart5Tx(uart5_tx_buff);
}


